﻿package {
	import flash.display.Sprite;
	import flash.display.Loader;
	import flash.display.Shape;
	import flash.net.URLRequest;
	import flash.display.Graphics;
	import flash.events.*;
	import fl.transitions.Tween;
	import fl.transitions.easing.Bounce;
	import flash.media.Sound;
	public class DynamicMask extends Sprite {
        public var orderNum:uint=0;
		public function DynamicMask(style:uint,linethink:Number,lineColor:uint,lineAlpha:Number,frameColor:uint,frameAlpha:Number,maskColor:uint,maskAlpha:Number,photoUrl:String) {
			const frameSize:uint=5;
			var photoW:uint;
			var photoH:uint;

			var tweenScaleX:Tween;
            var tweenScaleY:Tween;
			var photoLayer:Loader=new Loader();
			var maskLayer:Shape=new Shape();
			var frameLayer:Shape=new Shape();
			var urlRequest:URLRequest=new URLRequest(photoUrl);
			
			addChildAt(photoLayer,0);
			addChildAt(maskLayer,1);
			addChildAt(frameLayer,2);
			maskLayer.visible=false;
			photoLayer.load(urlRequest);
			photoLayer.contentLoaderInfo.addEventListener(Event.INIT,initHandler);
			photoLayer.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,progressHandler);
			photoLayer.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler);
			function initHandler(evt:Event) {
				photoW=evt.target.width;
				photoH=evt.target.height;
				with (frameLayer) {
					graphics.beginFill(frameColor,frameAlpha);
					//graphics.lineStyle(1,0x000000,0);
					graphics.moveTo(-(photoW/2+frameSize),-photoH/2);
					graphics.curveTo(-(photoW/2+frameSize),-(photoH/2+frameSize),-photoW/2,-(photoH/2+frameSize));
					graphics.lineTo(photoW/2,-(photoH/2+frameSize));
					graphics.curveTo(photoW/2+frameSize,-(photoH/2+frameSize),photoW/2+frameSize,-photoH/2);
					graphics.endFill();

					graphics.beginFill(frameColor,frameAlpha);
					graphics.moveTo(photoW/2+frameSize,-photoH/2);
					graphics.lineTo(photoW/2+frameSize,photoH/2);
					graphics.lineTo(photoW/2,photoH/2);
					graphics.lineTo(photoW/2,-photoH/2);
					graphics.endFill();

					graphics.beginFill(frameColor,frameAlpha);
					graphics.moveTo(photoW/2+frameSize,photoH/2);
					graphics.curveTo(photoW/2+frameSize,photoH/2+frameSize,photoW/2,photoH/2+frameSize);
					graphics.lineTo(-photoW/2,photoH/2+frameSize);
					graphics.curveTo(-(photoW/2+frameSize),photoH/2+frameSize,-(photoW/2+frameSize),photoH/2);
					graphics.endFill();

					graphics.beginFill(frameColor,frameAlpha);
					graphics.moveTo(-(photoW/2+frameSize),photoH/2);
					graphics.lineTo(-(photoW/2+frameSize),-photoH/2);
					graphics.lineTo(-photoW/2,-photoH/2);
					graphics.lineTo(-photoW/2,photoH/2);
					graphics.endFill();
				}
				//在frameLayer上画出边框
				with (frameLayer) {
					graphics.lineStyle(linethink,lineColor,lineAlpha,true);
					graphics.moveTo(-(photoW/2+frameSize),-photoH/2);
					graphics.curveTo(-(photoW/2+frameSize),-(photoH/2+frameSize),-photoW/2,-(photoH/2+frameSize));
					graphics.lineTo(photoW/2,-(photoH/2+frameSize));
					graphics.curveTo(photoW/2+frameSize,-(photoH/2+frameSize),photoW/2+frameSize,-photoH/2);
					graphics.lineTo(photoW/2+frameSize,photoH/2);
					graphics.curveTo(photoW/2+frameSize,photoH/2+frameSize,photoW/2,photoH/2+frameSize);
					graphics.lineTo(-photoW/2,photoH/2+frameSize);
					graphics.curveTo(-(photoW/2+frameSize),photoH/2+frameSize,-(photoW/2+frameSize),photoH/2);
					graphics.lineTo(-(photoW/2+frameSize),-photoH/2);

				}
				with (maskLayer) {
					graphics.beginFill(maskColor,maskAlpha);
					graphics.drawRect(-photoW/2,-photoH/2,photoW,photoH);
					graphics.endFill();
				}
				photoLayer.x=-photoW/2;
				photoLayer.y=-photoH/2;

			}
			function progressHandler(evt:Event) {

			}
			function completeHandler(evt:Event) {

			}
			photoLayer.addEventListener(MouseEvent.ROLL_OVER,overHandler);
			photoLayer.addEventListener(MouseEvent.ROLL_OUT,outHandler);
			function overHandler(evt:Event) {
				if (! maskLayer.visible) {
					maskLayer.visible=true;
				}
				if (style==1) {
					tweenScaleX=new Tween(maskLayer,"scaleX",Bounce.easeOut,0,1,1,true);
				} else if (style==2) {
					tweenScaleY=new Tween(maskLayer,"scaleY",Bounce.easeOut,0,1,1,true);
				} else {
					tweenScaleX=new Tween(maskLayer,"scaleX",Bounce.easeOut,0,1,1,true);
					tweenScaleY=new Tween(maskLayer,"scaleY",Bounce.easeOut,0,1,1,true);
				}
			}
			function outHandler(evt:Event) {
				if (style==1) {
					tweenScaleX.yoyo();
				} else if (style==2) {
					tweenScaleY.yoyo();
				} else {
					tweenScaleX.yoyo();
					tweenScaleY.yoyo();
				}
			}
		}
	}
}